/**
 * Copyright (c) 2018-2028, Chill Zhuang 庄骞 (smallchill@163.com).
 * <p>
 * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0;
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.gnu.org/licenses/lgpl.html
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.tmq.diancan.controller;

import com.tmq.diancan.entity.DiancanProductCategory;
import com.tmq.diancan.service.IDiancanProductCategoryService;
import com.tmq.diancan.vo.DiancanPdCtVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import lombok.AllArgsConstructor;
import javax.validation.Valid;

import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.ClassUtil;
import org.springblade.core.tool.utils.Func;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.tmq.diancan.entity.DiancanProductInfo;
import com.tmq.diancan.vo.DiancanProductInfoVO;
import com.tmq.diancan.service.IDiancanProductInfoService;
import springfox.documentation.annotations.ApiIgnore;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * 商品表 控制器
 *
 * @author tangmqb
 * @since 2020-01-10
 */
@RestController
@AllArgsConstructor
@RequestMapping("/diancanproductinfo")
@Api(value = "商品表", tags = "商品表接口")
public class DiancanProductInfoController {

	private IDiancanProductInfoService diancanProductInfoService;
	private IDiancanProductCategoryService diancanProductCategoryService;

	/**
	 * 微信小程序 获取分类商品列表及节点数据
	 * @return
	 */
	@RequestMapping("/category/list")
	public R<List<DiancanPdCtVo>> getPdCtVo() {
		//1. 查询所有的上架商品
		List<DiancanProductInfo> list = diancanProductInfoService.list();
		List<Integer> collect = list.stream().map(e -> e.getCategoryType()).collect(Collectors.toList());
		Collection<DiancanProductCategory> ctList = diancanProductCategoryService.listByIds(collect);
		//3. 数据拼装
		List<DiancanPdCtVo> productVOList = new ArrayList<>();
		for (DiancanProductCategory ct:ctList) {
			DiancanPdCtVo diancanPdCtVo = new DiancanPdCtVo();
			diancanPdCtVo.setType(ct.getCategoryId());
			diancanPdCtVo.setName(ct.getCategoryName());
			List<DiancanProductInfoVO> productInfoVOList = new ArrayList<>();
			for (DiancanProductInfo productInfo:list) {
				if (Func.equals(productInfo.getCategoryType(), ct.getCategoryId())) {
					DiancanProductInfoVO copy = Func.copy(productInfo,DiancanProductInfoVO.class);
					productInfoVOList.add(copy);
				}
			}
			diancanPdCtVo.setProductInfos(productInfoVOList);
			productVOList.add(diancanPdCtVo);
		}
		return R.data(productVOList);
	}

	/**
	 * 详情
	 */
	@GetMapping("/detail")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "详情", notes = "传入diancanProductInfo")
	public R<DiancanProductInfo> detail(DiancanProductInfo diancanProductInfo) {
		DiancanProductInfo detail = diancanProductInfoService.getOne(Condition.getQueryWrapper(diancanProductInfo));
		return R.data(detail);
	}

	/**
	 * 分页 商品表
	 */
	@GetMapping("/list")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "分页", notes = "传入diancanProductInfo")
	public R<IPage<DiancanProductInfo>> list(@ApiIgnore @RequestParam Map<String,Object> queryMap, Query query) {
		IPage<DiancanProductInfo> pages = diancanProductInfoService.page(Condition.getPage(query), Condition.getQueryWrapper(queryMap,DiancanProductInfo.class).lambda().orderByAsc(DiancanProductInfo::getCategorySort));
		return R.data(pages);
	}

	/**
	 * 自定义分页 商品表
	 */
	@GetMapping("/page")
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "分页", notes = "传入diancanProductInfo")
	public R<IPage<DiancanProductInfoVO>> page(DiancanProductInfoVO diancanProductInfo, Query query) {
		IPage<DiancanProductInfoVO> pages = diancanProductInfoService.selectDiancanProductInfoPage(Condition.getPage(query), diancanProductInfo);
		return R.data(pages);
	}

	/**
	 * 新增 商品表
	 */
	@PostMapping("/save")
	@ApiOperationSupport(order = 4)
	@ApiOperation(value = "新增", notes = "传入diancanProductInfo")
	public R save(@Valid @RequestBody DiancanProductInfo diancanProductInfo) {
		return R.status(diancanProductInfoService.save(diancanProductInfo));
	}

	/**
	 * 修改 商品表
	 */
	@PostMapping("/update")
	@ApiOperationSupport(order = 5)
	@ApiOperation(value = "修改", notes = "传入diancanProductInfo")
	public R update(@Valid @RequestBody DiancanProductInfo diancanProductInfo) {
		return R.status(diancanProductInfoService.updateById(diancanProductInfo));
	}

	/**
	 * 新增或修改 商品表
	 */
	@PostMapping("/submit")
	@ApiOperationSupport(order = 6)
	@ApiOperation(value = "新增或修改", notes = "传入diancanProductInfo")
	public R submit(@Valid @RequestBody DiancanProductInfo diancanProductInfo) {
		return R.status(diancanProductInfoService.saveOrUpdate(diancanProductInfo));
	}

	
	/**
	 * 删除 商品表
	 */
	@PostMapping("/remove")
	@ApiOperationSupport(order = 8)
	@ApiOperation(value = "删除", notes = "传入ids")
	public R remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
		return R.status(diancanProductInfoService.removeByIds(Func.toIntList(ids)));
	}

	
}
